<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class WayUserDB extends BaseDB
{
    /**
     * 作品素材
     */

    protected $twu = 'way_user';

    public function __construct()
    {
    }

    /**
     * APP作品
     */
    public function getAppIndex($page,$size,$stamp)
    {
        $query = DB::table($this->twu)->where('del',0)
            ->where('stamp',$stamp)->whereIn('status',[2,3,4]);
        $total = $query->count();
        $models = $query->skip($page)->take($size)
            ->orderBy('status','asc')->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $updateTime = '';
            if ($model['finishTime']) { $updateTime = date('Y-m-d H:i',$model['finishTime']); }
            if ($model['execTime']) { $updateTime = date('Y-m-d H:i',$model['execTime']); }
            if ($model['applyTime']) { $updateTime = date('Y-m-d H:i',$model['applyTime']); }
            $items[] = [
                'id'=>$model['id'], 'dn'=>env('DOMAIN').'/',
                'cover'=>$model['cover'], 'link'=>$model['link'],
                'status'=>$model['status'], 'updateTime'=>$updateTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * APP素材
     */
    public function getAppOne($stamp,$way)
    {
        $models = DB::table($this->twu)->where('del',0)
            ->where('stamp',$stamp)->where('way',$way)
            ->orderBy('id','desc')->skip(0)->take(1)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $items = array(); $dn = env('DOMAIN').'/';
        if ($file=$model['files']) {
            $jsons = json_decode($file, true); $items = $jsons;
            foreach ($jsons as $k => $json) {
//                if ($json['cover']) {
//                    $items[$k]['cover'] = $json['cover'] ? $dn.$json['cover'] : '';
//                }
//                if ($json['link']) {
//                    $items[$k]['link'] = $json['link'] ? $dn.$json['link'] : '';
//                }
                $jsons[$k]['dn'] = $dn;
            }
        }
        return array('code'=>'0','msg'=>'success','data'=>$model,'files'=>$items);
    }

    /**
     * APP未合成
     */
    public function getAppOneNoRender($stamp,$way)
    {
        $models = DB::table($this->twu)->where('del',0)
            ->where('stamp',$stamp)->where('way',$way)->where('status',1)
            ->orderBy('id','desc')->skip(0)->take(1)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $items = array(); $dn = env('DOMAIN').'/';
        if ($file=$model['files']) {
            $jsons = json_decode($file, true); $items = $jsons;
            foreach ($jsons as $k => $json) {
//                if ($json['cover']) {
//                    $items[$k]['cover'] = $json['cover'] ? $dn.$json['cover'] : '';
//                }
//                if ($json['link']) {
//                    $items[$k]['link'] = $json['link'] ? $dn.$json['link'] : '';
//                }
                $items[$k]['dn'] = $dn;
            }
        }
        return array('code'=>'0','msg'=>'success','data'=>$model,'files'=>$items);
    }

    /**
     * APP未合成
     */
    public function getAppLastOneNoRender()
    {
        $models = DB::table($this->twu)->where('del',0)->where('status',2)
            ->orderBy('id','asc')->skip(0)->take(1)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $items = array(); $dn = env('DOMAIN').'/';
        if ($file=$model['files']) {
            $jsons = json_decode($file, true); $items = $jsons;
            foreach ($jsons as $k => $json) {
//                if ($json['cover']) {
//                    $items[$k]['cover'] = $json['cover'] ? $dn.$json['cover'] : '';
//                }
//                if ($json['link']) {
//                    $items[$k]['link'] = $json['link'] ? $dn.$json['link'] : '';
//                }
                $items[$k]['dn'] = $dn;
            }
        }
//        $items = json_decode($model['files'], true);
        return array('code'=>'0','msg'=>'success','data'=>$model,'files'=>$items);
    }

    /**
     * APP一个
     */
    public function getOne($id)
    {
        $models = DB::table($this->twu)->where('id',$id)
            ->orderBy('id','desc')->skip(0)->take(1)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        return array('code'=>'0','msg'=>'success','data'=>$model);
    }

    /**
     * APP新增文件
     */
    public function setAppFileAdd($stamp,$way,$files)
    {
        $files = is_array($files) ? json_encode($files, JSON_UNESCAPED_UNICODE) : $files;
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->twu)->insertGetId([
                'stamp'=>$stamp, 'way'=>$way, 'files'=>$files, 'created_at'=>time(),
            ]);
            if (!$lastId) { return array('code'=>'-2', 'msg'=>'新增失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功', 'data'=>$lastId);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * APP更新文件
     */
    public function setAppFileUpdate($id,$files)
    {
        $files = is_array($files) ? json_encode($files, JSON_UNESCAPED_UNICODE) : $files;
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->twu)->where('id',$id)->update([
                'files'=>$files, 'updated_at'=>time(),
            ]);
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * APP申请合成
     */
    public function setAppApply($id,$bgmusic)
    {
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->twu)->where('id',$id)->update([
                'status'=>2, 'bgmusic'=>$bgmusic, 'applyTime'=>time(),
            ]);
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * APP合成完成
     */
    public function setAppFinish($id,$status,$cover,$link)
    {
        $update = [ 'cover'=>$cover, 'link'=>$link, ];
        if ($status==3) {
            $update['status'] = $status;
            $update['execTime'] = time();
        } else if ($status==4) {
            $update['status'] = $status;
            $update['finishTime'] = time();
        } else {
            return array('code'=>'-2','msg'=>'status error');
        }
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->twu)->where('id',$id)->update($update);
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }
}